package com.clps.test;

public class StringThread  implements  Runnable{

    private  static  final  String LOCK_PREFIX = "XXXX-----";

    private  String taskNo;

    public StringThread(String taskNo) {
        this.taskNo = taskNo;
    }

    @Override
    public void run() {
       String lock =  buildLock();
       synchronized (lock){
           System.out.println("["+Thread.currentThread().getName()+"]开始运行了");
           //休眠5s中
           try {
               Thread.sleep(500);
           } catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
           System.out.println("["+Thread.currentThread().getName()+"]结束运行");
       }
    }

    private String buildLock() {
        StringBuilder sb = new StringBuilder();
        sb.append(LOCK_PREFIX);
        sb.append(taskNo);
        String lock = sb.toString().intern();
        System.out.println("["+Thread.currentThread().getName()+"]构建了锁["+lock+"]");
        return lock;
    }
}
